#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
#include <vector>

namespace cmt
{
    template <typename T>
    auto readMyBin(const std::string& binPath)
    {
        using namespace std;

        ifstream ifs;
        ifs.open(binPath, std::ios::binary);
        ifs.seekg(0, ifs.end);
        const int length = ifs.tellg();
        ifs.seekg(0, ifs.beg);
        const int arrayLen = length / sizeof(T);
        std::shared_ptr<T[]> buffer(new T[arrayLen], [](T* p) { delete[] p; });
        ifs.read((char*)buffer.get(), length);
        ifs.close();

        std::vector<T> data(arrayLen);
        for (int i = 0; i < arrayLen; ++i) data[i] = buffer.get()[i];

        return move(data);
    }

    template <typename T>
    void writeMyBin(const std::string& binPath, const std::vector<T>& data)
    {
        using namespace std;
        ofstream ofs;
        ofs.open(binPath, std::ios::binary);
        ofs.write(reinterpret_cast<const char*>(&data[0]), sizeof(T) * data.size());
        ofs.close();
    }

    template <typename T>
    void writeMyBin(const std::string& binPath, T* data, size_t data_size)
    {
        using namespace std;
        ofstream ofs;
        ofs.open(binPath, std::ios::binary);
        ofs.write(reinterpret_cast<const char*>(data), sizeof(T) * data_size);
        ofs.close();
    }
}
